package com.qczl.pme.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qczl.pme.dao.AreaDao;
import com.qczl.pme.dao.CameraDao;
import com.qczl.pme.dao.LiveDao;
import com.qczl.pme.dao.LiveVideoDao;
import com.qczl.pme.dao.StbDao;
import com.qczl.pme.dao.TopicDao;
import com.qczl.pme.entity.PMEArea;
import com.qczl.pme.entity.PMECamera;
import com.qczl.pme.entity.PMELive;
import com.qczl.pme.entity.PMELiveVideo;
import com.qczl.pme.entity.PMESTB;
import com.qczl.pme.entity.PMESewise;
import com.qczl.pme.entity.PMETopic;
import com.qczl.pme.service.LiveService;
import com.qczl.pme.util.SewiseUtil;
import com.qczl.pme.vo.LiveVo;

/**
 * 直播业务逻辑层实现类
 * 
 * @作者:SongR
 * @日期:2017年9月21日下午1:58:54
 */
@Service
public class LiveServiceImpl implements LiveService {

	@Autowired
	LiveDao dao;
	@Autowired
	StbDao stbdao;
	@Autowired
	CameraDao cameradao;
	@Autowired
	TopicDao topicdao;
	@Autowired
	AreaDao areadao;
	@Autowired
	LiveVideoDao lvdao;

	/**
	 * 实现开始直播
	 */
	@Override
	public String startLiveV1(String stbno, String topicid, String[] typeid) throws Exception {
		PMESTB stb = stbdao.get("from PMESTB where no ='" + stbno + "' and status=0");
		PMECamera camera = cameradao.get(PMECamera.class, stb.getCameraid());
		PMESewise sewise = camera.getSewise();
		PMETopic topic = topicdao.get(PMETopic.class, new Long(topicid));
		PMELiveVideo video = new PMELiveVideo();
		JSONObject j = SewiseUtil.recording(sewise, camera.getStreamId(), "sewise.livecallback");
		if (null != j && j.getString("success") == "true") {
			video.setCameraid(camera.getId());
			video.setCreatTime(new Date());
			video.setStartTime(new Date());
			video.setName(topic.getName());
			video.setTaskId(j.getString("taskid"));
			video.setStatus(1);
			Long id = (Long) lvdao.save(video);
			PMELive live = new PMELive();
			live.setVideoId(id);
			live.setStbno(stbno);
			live.setName(topic.getName());
			live.setStarttime(new Date());
			live.setStatus(1);
			live.setTopicid(new Long(topicid));
			dao.save(live);
			video.setTaskId(j.getString("taskid"));
		} else {

		}
		return camera.getUrl() + "?taskid=" + video.getTaskId() + "&cameraid=" + camera.getId() + "&type=live";
	}

	/**
	 * 实现获取直播列表
	 */
	@Override
	public List<LiveVo> getLiveV1(String stbno) throws Exception {
		PMESTB stb = stbdao.get("from PMESTB where no ='" + stbno + "' and status=0");
		PMEArea area = stb.getArea();
		String hql = "from PMELive where status=1";
		if (area.getLevel() == 0) {
		} else {
			PMEArea a = areadao.get("from PMEArea where id='" + area.getPid() + "'");
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			hql += " and stbno='" + s.getNo() + "'";
		}
		List<PMELive> lives = dao.find(hql);
		List<LiveVo> vos = new ArrayList<LiveVo>();
		for (PMELive l : lives) {
			LiveVo vo = new LiveVo();
			PMESTB s = stbdao.get("from PMESTB where no ='" + l.getStbno() + "' and status=0");
			PMETopic topic = topicdao.get(PMETopic.class, new Long(l.getTopicid()));
			PMECamera camera = cameradao.get(PMECamera.class, s.getCameraid());
			vo.setName(topic.getName());
			vo.setUrl(camera.getUrl());
			vo.setStarttime(l.getStarttime());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 获取直播回看
	 */
	@Override
	public List<LiveVo> getBackV1(String stbno, String topicid, String start, String size) throws Exception {
		PMESTB stb = stbdao.get("from PMESTB where no ='" + stbno + "' and status=0");
		PMEArea area = stb.getArea();
		String hql = "from PMELive where status=0 and  topicid=" + topicid;
		if (area.getLevel() == 0) {
		} else {
			PMEArea a = areadao.get("from PMEArea where id='" + area.getPid() + "'");
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			hql += " and stbno='" + s.getNo() + "'";
		}
		List<PMELive> lives = new ArrayList<PMELive>();
		if (start == null || start.equals("")) {
			lives = dao.find(hql);
		} else {
			lives = dao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		}
		List<LiveVo> vos = new ArrayList<LiveVo>();
		for (PMELive l : lives) {
			LiveVo vo = new LiveVo();
			PMESTB s = stbdao.get("from PMESTB where no ='" + l.getStbno() + "' and status=0");
			PMETopic topic = topicdao.get(PMETopic.class, new Long(l.getTopicid()));
			PMECamera camera = cameradao.get(PMECamera.class, s.getCameraid());
			PMELiveVideo lv = lvdao.get(PMELiveVideo.class, l.getVideoId());
			vo.setName(topic.getName());
			String url = "http://" + camera.getSewise().getIp();
			if (camera.getSewise().getProt2() != null && !"".equals(camera.getSewise().getProt2())) {
				url += ":" + camera.getSewise().getProt2();
			}
			System.out.println(lv.getStartTime());
			vo.setUrl(url + lv.getPlayUrl());
			vo.setStarttime(lv.getStartTime());
			vo.setEndtime(lv.getEndTime());
			vos.add(vo);
		}
		return vos;
	}

}
